using System;
using System.Xml;
using DesignByContract;
using Outcoding.GraphGenerator.Entities;
using Outcoding.GraphGenerator.Util.Logging;

namespace Outcoding.GraphGenerator.Composition{
    public class MajorGridlineBuilder : BuilderBase{
        #region Attributes

        private string colorAttr = "color";
        private string styleAttr = "style";
        private string visibleAttr = "visible";
        private string weightAttr = "weight";

        #endregion

        private readonly string mNodeName = XmlTagNames.majorgridline.ToString();

        private XmlNode mNode;

        private string mNodePath{
            get { return "./" + mNodeName; }
        }

        public override void SelectNode(XmlDocument document){
            throw new Exception("The method or operation is not implemented.");
        }

        public override void Compose(){
            throw new Exception("The method or operation is not implemented.");
        }

        public override void Compose(XmlNode node){
            mNode = node.SelectSingleNode(mNodePath);
            try{
                BuildObject(mNode);
            }
            catch (PreconditionException preExc){
                LogManager.Instance.LogWarning("MinValue entity could not be created; process aborted.", preExc);
            }
        }

        public override void AppendToParent(object parent){
            Check.Require(parent is ValueAxis, "Parent object not a valid argument");
            (parent as ValueAxis).MajorGrid = (MajorGridline) mProduct;
        }

        private void BuildObject(XmlNode majorGridlineXmlNode){
            var value = new MajorGridline();
            try{
                if (majorGridlineXmlNode.Attributes[visibleAttr] != null)
                    value.IsVisible = ParseIsVisibleAttribute(majorGridlineXmlNode);
                if (majorGridlineXmlNode.Attributes[styleAttr] != null)
                    value.Line.Style = ParseStyleAttribute(majorGridlineXmlNode);
                if (majorGridlineXmlNode.Attributes[weightAttr] != null)
                    value.Line.Weight = ParseWeightAttribute(majorGridlineXmlNode);
                if (majorGridlineXmlNode.Attributes[colorAttr] != null)
                    value.Line.Color = ParseColorAttribute(majorGridlineXmlNode);
            }
            catch (PreconditionException preExc){
                //log error
                value = null;
                throw;
            }

            mProduct = value;
        }

        private bool ParseIsVisibleAttribute(XmlNode majorGridlineXmlNode){
            Check.Require(majorGridlineXmlNode.Attributes[visibleAttr] != null, "visible attribute not present");

            return Boolean.Parse(majorGridlineXmlNode.Attributes[visibleAttr].Value);
        }

        private XlLineStyle ParseStyleAttribute(XmlNode majorGridlineXmlNode){
            Check.Require(majorGridlineXmlNode.Attributes[styleAttr] != null, "style attribute not present");

            return XlLineStyleHelper.ConvertFromString(majorGridlineXmlNode.Attributes[styleAttr].Value);
        }

        private XlBorderWeight ParseWeightAttribute(XmlNode majorGridlineXmlNode){
            Check.Require(majorGridlineXmlNode.Attributes[weightAttr] != null, "weight attribute not present");

            return XlBorderWeightHelper.ConvertFromString(majorGridlineXmlNode.Attributes[weightAttr].Value);
        }

        private long ParseColorAttribute(XmlNode majorGridlineXmlNode){
            Check.Require(majorGridlineXmlNode.Attributes[colorAttr] != null, "color attribute not present");

            return Entities.Util.ColorTranslation(majorGridlineXmlNode.Attributes[colorAttr].Value);
        }
    }
}